package cn.coisini.service.impl;

import cn.coisini.mapper.UserSearchMapper;
import cn.coisini.model.common.dtos.Result;
import cn.coisini.model.common.enums.ResultEnum;
import cn.coisini.model.dto.UserSearchDto;
import cn.coisini.model.pojo.UserSearch;
import cn.coisini.service.UserSearchService;
import cn.coisini.utils.IdWorker;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static cn.coisini.model.common.constants.AdminConstants.FALSE;
import static cn.coisini.model.common.constants.AdminConstants.TRUE;
import static cn.coisini.utils.JwtUtil.getUserIdFromToken;

/**
 * @author xiangshaw
 * Description: 用户搜索服务实现类
 */
@Service
public class UserSearchServiceImpl extends ServiceImpl<UserSearchMapper, UserSearch> implements UserSearchService {
    // 获取用户搜索历史
    @Override
    public Result<List<UserSearch>> getUserSearch(UserSearchDto userSearchDto) {
        String userId = getUserId();
        if (CharSequenceUtil.isBlank(userId)) {
            return Result.ok(List.of());
        }
        Page<UserSearch> page = new Page<>(userSearchDto.getCurrent(), userSearchDto.getLimit());
        page(page, Wrappers.<UserSearch>lambdaQuery()
                .eq(UserSearch::getCreateUser, userId)
                .eq(UserSearch::getStatus, TRUE));
        return Result.ok(page.getRecords());
    }

    private static String getUserId() {
        return getUserIdFromToken();
    }

    // 删除用户搜索历史
    @Override
    public Result<ResultEnum> delUserSearch(UserSearchDto userSearchDto) {
        String userId = getUserId();
        if (CharSequenceUtil.isBlank(userId)) {
            return Result.error(ResultEnum.REQUIRES_LOGIN, "登录后数据才会保存");
        }
        update(Wrappers.<UserSearch>lambdaUpdate()
                .eq(UserSearch::getKeyword, userSearchDto.getKeyword())
                .eq(UserSearch::getCreateUser, userId)
                .set(UserSearch::getStatus, FALSE));
        return Result.ok(ResultEnum.SUCCESS);
    }

    // 添加用户搜索历史
    @Override
    public void addUserSearch(UserSearchDto userSearchDto) {
        String userId = getUserId();
        if (CharSequenceUtil.isBlank(userId)) {
            return;
        }
        // 1.查询当前搜索记录
        UserSearch userSearch = getOne(Wrappers.<UserSearch>lambdaQuery()
                .eq(UserSearch::getKeyword, userSearchDto.getKeyword())
                .eq(UserSearch::getCreateUser, getUserId()));
        // 2.如果存在且状态为1，直接返回；如果存在且状态为0，更新状态为1
        if (userSearch != null) {
            if (userSearch.getStatus().equals(true)) {
                return;
            } else {
                userSearch.setStatus(true);
                updateById(userSearch);
            }
        } else {
            // 3.如果不存在，添加新记录
            userSearch = new UserSearch();
            userSearch.setId(String.valueOf(new IdWorker().nextId()));
            userSearch.setCreateUser(userId);
            userSearch.setKeyword(userSearchDto.getKeyword());
            userSearch.setStatus(true);
            userSearch.setCreateTime(LocalDateTime.now());
            save(userSearch);
        }
    }

    // 清空用户搜索历史
    @Override
    public Result<ResultEnum> delUserSearchAll() {
        String userId = getUserId();
        if (CharSequenceUtil.isBlank(userId)) {
            return Result.error(ResultEnum.REQUIRES_LOGIN, "登录后数据才会保存");
        }
        update(Wrappers.<UserSearch>lambdaUpdate()
                .eq(UserSearch::getCreateUser, userId)
                .set(UserSearch::getStatus, FALSE));
        return Result.ok(ResultEnum.SUCCESS);
    }
}
